package file;

import java.io.File;

/**
 * @author wang hao
 * @date 2023-04-01 10:19
 */
public class Demo04 {
    // 搜索某个目录下的全部文件
    public static void searchFile(File file, String fileName){
        if (file == null || !file.isDirectory()){
            System.out.println("当前搜索的不是文件夹");
        }

        File[] files = file.listFiles();
        if (files != null && files.length > 0){
            for (File file1 : files) {
                if (file1.isDirectory())
                    searchFile(file1,fileName);
                else
                    if (file1.getName().equals(fileName))
                        System.out.println(file1.getAbsolutePath());
            }
        }

    }

    public static void main(String[] args) {
        // 根路径 查找 1.txt 文件
        searchFile(new File("/Users/wangwanghao/study/java/"),"1.txt");
    }

}
